#include <myheader_c_plus.h>
#include <functional>
using std::function;
using std::endl;
using std::cout;
using std::allocator;
template<typename T>
class Vector
{
public:
    //迭代器
    using iterator = T *;
    iterator begin()const
    {
        return _start;
    }

    iterator end()const
    {
        return _finish;
    }
    //构造函数
    Vector();
    ~Vector();

    void push_back(const T &);
    void pop_back();

    int size() const;
    int capacity() const;
    void resize();


private:
    void reallocate();//重新分配内存动态扩容
    static std::allocator<T> _alloc;//空间的申请与对象的构建分开

    T *_start;
    T * _finish;
    T * _end_of_storage;

};
template<typename T>
std::allocator<T>Vector<T>:: _alloc;//空间的申请与对象的构建分开
//函数实现
template<typename T>
Vector<T>::Vector()
:_start(nullptr)
,_finish(nullptr)
,_end_of_storage(nullptr)
{

}


template<typename T>
Vector<T>::~Vector()
{
    if(_start)
    {
        while(_finish != _start){
            _alloc.destroy(_finish);
            --_finish;
        }
        _alloc.deallocate(_start, capacity());
    }
}
template<typename T>
void Vector<T>::reallocate()
{
    T * ptemp;
    size_t oldcapacity = capacity();
    size_t newcapacity = oldcapacity * 2;
    //如果旧空间是0，则分配一个大小为8个元素大小的初始空间
    if(capacity() == 0)
    {
        ptemp = _alloc.allocate(1); 
    //将指针指向新空间
    _start = ptemp;
    _finish = _start;
    _end_of_storage = _start + 1;
    }
    else
    {
    //如果不为0，申请新空间，大小是旧空间的两倍
    ptemp = _alloc.allocate(newcapacity);

    //将元素复制到新空间
    int size = (_finish - _start) * sizeof(T);
    memcpy(ptemp, _start,size);
    //回收旧空间
    if(_start)
    {
        while(_finish != _start)
        {
            --_finish;
            _alloc.destroy(_finish);
        }
        _alloc.deallocate(_start, oldcapacity);
    }
    //将指针指向新空间
    _start = ptemp;
    _finish = _start + oldcapacity;
    _end_of_storage = _start + newcapacity;
    }

    
}
template<typename T>
void Vector<T>::push_back(const T & ele)
{
    //如果空间不够了，要进行扩孔
    if(size() == capacity()) 
    {
        reallocate();
    }
    //在尾部构建对象
    _alloc.construct(_finish, ele);
    ++_finish;
}


template<typename T>
void Vector<T>::pop_back()
{
    if(size() > 0)
    {
   //销毁尾部对象
    --_finish;
    _alloc.destroy(_finish);

    }
    
}
template<typename T>
int Vector<T>::size()const
{
    return _finish - _start;
}
template<typename T>
int Vector<T>::capacity()const
{
    return _end_of_storage - _start;
}

template<typename T>
void Vector<T>::resize()
{
    if(_finish != _end_of_storage)
    {
        _end_of_storage = _finish;
    }

}
int main(int argc, char *argv[])
{
    Vector<int> vec;
    cout << vec.capacity() << endl;
    vec.push_back(3);
    cout << vec.capacity() << endl;
    vec.push_back(3);
    cout << vec.capacity() << endl;
    vec.push_back(4);
    cout << vec.capacity() << endl;
    cout << "=====" << endl;
    Vector<int>::iterator it = vec.begin();
    for(; it != vec.end(); ++it)
    {
        cout << *it << endl;
    }
    vec.push_back(54);
    cout << vec.capacity() << endl;
    vec.push_back(35);
    cout << vec.capacity() << endl;
    vec.push_back(47);
    cout << vec.capacity() << endl;
    vec.push_back(30);
    cout << vec.capacity() << endl;
    vec.push_back(38);
    cout << vec.capacity() << endl;
    vec.push_back(44);
    cout << vec.capacity() << endl;
    cout << "============" << endl;
    vec.pop_back();
    cout << vec.capacity() << endl;
    vec.pop_back();
    cout << vec.capacity() << endl;
    vec.pop_back();
    cout << vec.capacity() << endl;
    vec.pop_back();
    cout << vec.capacity() << endl;
    vec.pop_back();
    cout << vec.capacity() << endl;
    vec.pop_back();
    cout << vec.capacity() << endl;
    vec.pop_back();
    cout << vec.capacity() << endl;
    vec.pop_back();
    cout << vec.capacity() << endl;
    cout << vec.size() << endl;
    vec.resize();
    cout << vec.capacity() << endl;
    return 0;
}

